<?php

class tx_Main_Hook_ConvertLinksHook {
	public function main($param, $obj) {
		// replacneme interne urlky s ?id= za normalne
		$pattern = '@href="index.php\?id=(.*?)"@';
		preg_match_all($pattern, $obj->content, $matches);

		$uniqueMatches = array_unique($matches[0]);
		$uniquePageAliasList = array_unique($matches[1]);

		$pageRepository = new PageRepository();
		$pageList = $pageRepository->getByAlias($uniquePageAliasList);
		
		$internalPages = array();
		foreach ($pageList as $page) {
			$internalPages[$page['alias']] = $page;
		}
		
		for ($i = 0; $i < count($uniqueMatches); $i++) {
			if (isset($internalPages[$uniquePageAliasList[$i]])) {
				$oldHref = $uniqueMatches[$i];
				$newHref = 'href="' . $internalPages[$uniquePageAliasList[$i]]->getUrl() . '"';
				$obj->content = str_replace($oldHref, $newHref, $obj->content);
			}
		}
	}
}

?>
